<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Seller extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */

    public function __construct()
    {
        parent::__construct();
            // Your own constructor code
    } 
    
    
    
    // bune tuna ..
	public function index()
	{
       $this->login();
	}
  public function adet_hatasi($str)
    {
        
        

        if ($str >= ($this->input->post('adet')-1))
        {
           	$this->form_validation->set_message('adet_hatasi', '1. Aralik adet '.($this->input->post('adet')-1).' sayısından küçük olmalı');
			return FALSE; 
        }
        else
        {
              
			return TRUE;
        }

    }      
     public function price_hatasi($str)
    {
        
        

        if ($str > $this->input->post('price1'))
        {
           	$this->form_validation->set_message('price_hatasi', '2. Aralik fiyatı 1. Aralık fiyatından büyük olmamalı');
			return FALSE; 
        }
        else
        {
              
			return TRUE;
        }

    }       
    public function add_to_sell()
    {
     /*  echo "<pre>";
       var_dump($this->input->post());
       die(); */
       
      
        $product_id = $this->input->post('product_id');
        $product_detail = $this->product_model->get_product($product_id)->row(0);
        $pname = str_replace(' ', '-', $product_detail->name);
        $pname = mb_strtolower($pname,'UTF-8');
        
         $this->form_validation->set_rules('fiyat', 'Fiyat', 'required|xss_clean|numeric');
        $this->form_validation->set_rules('adet', 'Adet', 'required|xss_clean|numeric');
        $this->form_validation->set_rules('kargo','Kargo','required|xss_clean');
          $this->form_validation->set_rules('fiyat11');
                     $this->form_validation->set_rules('fiyat21');
                     $this->form_validation->set_rules('fiyat22');
        if($this->input->post('cars') == 'aralikli_fiyat')
        {
                     $this->form_validation->set_rules('fiyat11');
                     $this->form_validation->set_rules('fiyat21');
                     $this->form_validation->set_rules('fiyat22');
                     $this->form_validation->set_rules('fiyat12', '1. Aralık Adet', 'required|xss_clean|numeric|callback_adet_hatasi');
              $this->form_validation->set_rules('price1', '2. Aralik Fiyat', 'required|xss_clean|numeric');
             
        
                $this->form_validation->set_rules('price2', '2. Aralik Fiyat', 'required|xss_clean|numeric|callback_price_hatasi');
        }
    
        
        if ($this->form_validation->run() == FALSE)
    		{
                $data['product'] = $this->product_model->get_product($product_id)->row(0);
                $data['category_name'] = $this->config_model->get_category_name($data['product']->category_id)->row(0)->name;
                $data['subcategory_name'] = $this->config_model->get_subcategory_name($data['product']->subcategory_id)->row(0)->name;
        
        
                $data['subview'] = 'view_product_detail';
                $this->load->view('layouts/default1',$data); 
               
    		}
    		else
    		{
    		    if($this->input->post('cars') == "normal_fiyat")
                {
                    $fiyat_aralik_id = 0;
                }
                else
                {
                         $fiyat_araligi = array(
                        'aralik11' => $this->input->post('fiyat11'),
                        'aralik12' => $this->input->post('fiyat12'),
                          'aralik21' => $this->input->post('fiyat21'),
                            'aralik22' => $this->input->post('fiyat22'),
                              'fiyat1' => $this->input->post('price1'),
                                'fiyat2' => $this->input->post('price2'),
                      );
                      $this->db->insert('fiyat_araligi',$fiyat_araligi);
                      $fiyat_aralik_id =  $this->db->insert_id();
                }
    
              
    		  $insert_data = array(
                'user_id' => $this->session->userdata('user_id'),
                'product_id' => $this->input->post('product_id'),
                'user_price' => $this->input->post('fiyat'),
                'stock' =>  $this->input->post('adet'),
                'mod'   => 0,
                'kargo' => $this->input->post('kargo'),
                'fiyat_araligi_id' => $fiyat_aralik_id,
                'dateadd' => date("Y-m-d H:i:s"),
                'dateupd' => date("Y-m-d H:i:s")
                
              
              );
              if($this->db->insert('seller',$insert_data))
              {
                      $this->session->set_flashdata('successmsg','Ürününü Başarıyla eklenmiştir');
    		          redirect('main/message');
              }
              else
              {
                  $this->session->set_flashdata('errormsg','Ürününü eklenemedi seller line 84');
    		          redirect('main/message');
              }
              // aynı üründen farklı fiyatlarla girme durumu var..
         
            }
    }
    
 public function get_adres_tipi()
    {
        
        //die();
            if((int)$this->input->post('adrestipi') === 1)
            {
                $this->session->set_userdata('adrestipi1',true);
                $this->session->set_userdata('adrestipi2',false);
            }
            if((int)$this->input->post('adrestipi') === 2)
            {
                $this->session->set_userdata('adrestipi2',true);
                $this->session->set_userdata('adrestipi1',false);
            }
            $this->user_details();
    } 
    public function user_details()
    {
        $user_id = $this->session->userdata('user_id');
        if($this->user_model->get_user_information($user_id))
        {
            $data['user_information'] = $this->user_model->get_user_information($user_id);
            // dier stepe yonlendir 
            //$this->payment();
            redirect('kategoriler');
        }
        else
        {
         


            
            
            if($this->session->userdata('adrestipi1'))
            {
                 
               /* $data['subview'] = 'checkout_part2_sahis';
    	        $this->load->view('layouts/default1',$data);*/
                $this->user_details_sahis();
            }
            if($this->session->userdata('adrestipi2'))
            {
                
             /*   $data['subview'] = 'checkout_part2_tuzel';
    	        $this->load->view('layouts/default1',$data); */
                  $this->user_details_tuzel();
            }
            $adrestipi = $this->session->userdata('adrestipi2');
            if(!$adrestipi)
            {
             
               
                  $this->user_details_sahis();
              /*  $data['subview'] = 'checkout_part2_sahis';
    	        $this->load->view('layouts/default1',$data);*/
            }
           

        }
     
    }
    public function user_details_sahis()
    {
        
            $this->form_validation->set_rules('detail_type', 'Kullanıcı tipi', 'required|xss_clean');
            $this->form_validation->set_rules('vergino', 'Vergi No', 'required|xss_clean|numeric|exact_length[10]');
            $this->form_validation->set_rules('tcno', 'TC Kimlik No', 'required|xss_clean|numeric|exact_length[11]');
            $this->form_validation->set_rules('vergidno','Vergi daiersi', 'required|xss_clean');
            $this->form_validation->set_rules('vergiil','Vergi İl', 'required|xss_clean');
            $this->form_validation->set_rules('adres','Adres', 'required|xss_clean');
            $this->form_validation->set_rules('evtel');
            $this->form_validation->set_rules('ceptel');
            
        /*    echo "<pre>";
            var_dump($this->input->post());
            echo "</pre>";
            die(); */
            
      		if ($this->form_validation->run() == FALSE)
    		{
    		    $data['subview'] = 'seller_part2_sahis';
    	        $this->load->view('layouts/default1',$data);
    		}
    		else
    		{
                 $sahis_info = array(
                    'user_id' => $this->session->userdata('user_id'),
                    'tcno'  =>  $this->input->post('tcno'),
                    'vergino'   =>  $this->input->post('vergino'),
                    'vergidno'  =>  $this->input->post('vergidno'),
                    'vergiil'   =>  $this->input->post('vergiil'),
                    'adres' =>  $this->input->post('adres'),
                    'evtel' =>  $this->input->post('evtel'),
                    'ceptel'    =>  $this->input->post('ceptel'),
                    'dateadd'   => date("Y-m-d H:i:s"),
                    'dateupd'   =>  date("Y-m-d H:i:s")
                 );
                 $this->db->insert('user_sahis',$sahis_info);
                 $this->db->where('user_id', $this->session->userdata('user_id'));
                 $this->db->update('users', array('detail_type' => 1)); 
                
                redirect('kategoriler');
            } 
        
    }
    public function user_details_tuzel()
    {
     
          $this->form_validation->set_rules('detail_type', 'Kullanıcı tipi', 'required|xss_clean');
            $this->form_validation->set_rules('vergino', 'Vergi No', 'required|xss_clean|numeric|exact_length[10]');
            
            $this->form_validation->set_rules('vergidno','Vergi daiersi', 'required|xss_clean');
            $this->form_validation->set_rules('vergiil','Vergi İl', 'required|xss_clean');
            $this->form_validation->set_rules('adres','Adres', 'required|xss_clean');
            $this->form_validation->set_rules('telefon','Telefon','required|xss_clean');
            $this->form_validation->set_rules('email', 'Email','required|valid_email|xss_clean');
            
        /*    echo "<pre>";
            var_dump($this->input->post());
            echo "</pre>";
            die(); */
            
      		if ($this->form_validation->run() == FALSE)
    		{
    		  	
    		    $data['subview'] = 'seller_part2_tuzel';
    	        $this->load->view('layouts/default1',$data);
    		}
    		else
    		{
    		
                 $sahis_info = array(
                    'user_id' => $this->session->userdata('user_id'),
                    
                    'vergino'   =>  $this->input->post('vergino'),
                    'vergidno'  =>  $this->input->post('vergidno'),
                    'vergiil'   =>  $this->input->post('vergiil'),
                    'adres' =>  $this->input->post('adres'),
                    'telefon' =>  $this->input->post('telefon'),
                    'email'    =>  $this->input->post('email'),
                    'dateadd'   => date("Y-m-d H:i:s"),
                    'dateupd'   =>  date("Y-m-d H:i:s")
                 );
                 $this->db->insert('user_tuzel',$sahis_info);

                 $this->db->where('user_id', $this->session->userdata('user_id'));
                 $this->db->update('users', array('detail_type' => 2)); 
               redirect('kategoriler');
            }         
    }


    public function login()
    {
        
        
        // once bak loginmi deilse login et
       
        if($this->session->userdata('logged_in'))
        {
            $this->user_details();
        }
        else
        {
            
            
             $this->form_validation->set_rules('email', 'Email', 'required|valid_email|xss_clean');
            $this->form_validation->set_rules('password', 'Password', 'required|xss_clean|min_length[5]');
            
      		if ($this->form_validation->run() == FALSE)
    		{
    		    $data['subview'] = 'view_login4';
    	        $this->load->view('layouts/default1',$data);
    		}
    		else
    		{
                 // Get User Paramaters
    		     $email = $this->input->post('email');
                 $password = $this->input->post('password'); 
                 
                 // Use the User model
    			 
                 
                 $user_id = $this->user_model->check_login($email,$password);
                 
                
                 if(!$user_id)
                 {
                   
                    $this->session->set_flashdata('errormsg', "Kullanıcı adı veya şifre hatalı girildi.");
                    redirect('seller/login');
                 }
                 else 
                 {
                    $user_info = $this->user_model->get_user_details($user_id); 
                    if((int)$user_info->row(0)->status === PASSIVE)
                    {
                         $this->session->set_flashdata('alertmsg', "Hesabınız aktif edilmemiştir lütfen size gönderilen email ile aktifleştirin.");
                         redirect('seller/login');
                    }
                    else
                    {
                        if($this->db->where(array('cisession'=>$this->session->userdata['session_id'],'status' => 1))->get('cart')->num_rows()>0)
                        {
                            $cart_id = $this->db->where(array('cisession'=>$this->session->userdata['session_id'],'status' => 1))->get('cart')->row(0)->cart_id;
                            $this->db->where('cart_id',$cart_id);
                            $this->db->update('cart',array('customer_id'=>$user_info->row(0)->user_id));
                        }
                    	$this->session->set_userdata(array(
        								'logged_in' =>  true, 
        								'user_id'   =>  $user_info->row(0)->user_id,
                                        'email'     =>  $user_info->row(0)->email,
                                        'type'      =>  $user_info->row(0)->type
        								));
        
        				$this->user_details();                    
                        
                    }            				
    
                    
                 }
    		  }  
        }

        
        
        
        
    }

  
         
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */